import pandas as pd
def dataframe(k,pos):
    k = pd.DataFrame(k)
    k.index = k["timekey"]
    del k["_id"]
    k = k.sort_index()
    pos = pd.DataFrame(pos).T
    #pos.index=pos["date"]
    del pos["_id"]
    #pos = pos.sort_index()
    return k,pos
def concat(k,pos,name):
    k["pl"] = -k["close"].diff(-1)
    """
    k["pl_2"] = -k["close"].diff(-2)
    k["pl0"] = k["close"].diff(1)
    k["pl1"] = -k["vwap"].diff(-1)
    k["pl2"] = k["vwap"].diff(1)
    """
    n0 = len(pos)/23
    pos1 = pos.loc[(pos["member_name"] == name)]

    if not pos1.empty:
        pos1.index = pos1["date"]
        pos1 = pos1.sort_index()
        pos1=pos1.drop(list(pos1[pd.isna(pos1['short_position'])].index))
        pos1=pos1.drop(list(pos1[pd.isna(pos1['long_position'])].index))
        if n0-len(pos1)<=5:
            pos1["pos"]=pos1["long_position"] - pos1["short_position"]
            pos1["pos_diff"]=pos1["pos"].diff(1)
            pos1["long_diff"] = -pos1["long_position"].diff(-1)
            pos1["short_diff"] = -pos1["short_position"].diff(-1)
            pos2=pd.concat([pos1, k], axis=1, join="inner")
            pos2["x"] = pos2.apply(xx, axis=1)
            pos2["y"] = pos2.apply(yy, axis=1)
            pos2["x1"] = pos2.apply(xx1, axis=1)
            pos2["y1"] = pos2.apply(yy1, axis=1)
            return pos2
        else:
            return pd.DataFrame()
    else:
        return pd.DataFrame()
def xx(x):
    if x["long_diff"]>0 and x["short_diff"]<0:
        if abs(x["short_diff"])>x["long_diff"]:
            return "short_close"
        else:
            return "long_open"
    elif x["long_diff"]<0 and x["short_diff"]>0:
        if abs(x["long_diff"])>x["short_diff"]:
            return "long_close"
        else:
            return  "short_open"
    elif x["long_diff"]>0 and x["short_diff"]>0:
        if x["long_diff"]>x["short_diff"]:
            return "allopen_long"
        else:
            return  "allopen_short"
    elif x["long_diff"]<0 and x["short_diff"]<0:
        if x["long_diff"]>x["short_diff"]:
            return "allclose_short"
        else:
            return  "allclose_long"
def xx1(x):
    if x["x"] in ["long_open","short_close","allopen_long","allclose_short"]:
        return "long"
    elif x["x"] in ["short_open","long_close","allclose_long","allopen_short"]:
        return "short"
    else:
        return "unkown"
def yy(x):
    if x["pl"]>=10:
        return "up"
    elif x["pl"]<=-10:
        return "down"
    else:
        return "hold"
def yy1(x):
    if x["pl"]<0:
        return "noup"
    elif x["pl"]>0:
        return "nodown"
    else:
        return "hold"
"""
objs=["rb1710","rb1801","rb1805","rb1810","rb1901","rb1905"]
from jili.report import html
report=html.text("Positon_AR_Analysis")
reload(getdata)
for obj in objs:
    for name in names:
        pos = getdata.getpos(obj,name)
        if not pos.empty:
            r, rs, rr = tagcalc.AR_analysis(pos, {
                "x": ["long_open", "long_close", "short_close", "short_open", "allopen_long", "allopen_short",
                      "allclose_long", "allclose_short"]}, {"y": ["up", "hold", "down"]})
            rs_df = pd.DataFrame(rs).T
            report.addtext(obj+"-"+name)
            report.addtable(rs_df)
        else:
            print(obj,name)
 
import pandas as pd
from jili.calc import tagcalc
from jili.data import getdata
k, pos = getdata.load("rb1710")
pos1=getdata.concat(k,pos,"永安期货")
reload(tagcalc)
r,rv=tagcalc.AR_reseach(pos1,{"x":["long_open","long_close","short_close","short_open","allopen_long","allopen_short","allclose_long","allclose_short"]},{"y":["up","hold","down"]})
r_df=pd.DataFrame(r).T
rv_df=pd.DataFrame(rv).T
"""
